<?php
// -- webMS Modification --
  // Ensure a valid runtime environment or die
  if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

  // -- webMS Modification --
  // Check if osCommerce environment is already loaded
  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {
      // Reconnect to osCommerce Database
      tep_db_connect() or die('Unable to connect to database server!');
    }
    // Load osCommerce Globals for Boxes
    global $category_links, $languages_id, $catID, $catID_array, $current_category_id;
    global $request_type, $lng, $cart, $currencies, $language, $mosUser,  $navigation, $mosConfig_mailfrom, $breadcrumb;
  } else {
    // Load osCommerce Environment for the First Time
    require('components/'.$option.'/includes/application_top.php');
  }
  $Qcountry_Query = tep_db_query("Select countries_name from ".TABLE_COUNTRIES." where countries_id = '".STORE_COUNTRY."'");
    $Qcountry_Array = tep_db_fetch_array($Qcountry_Query);
    $Qcountry = $Qcountry_Array['countries_name'];

  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_QUOTE);

  if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'send' && !isset($_REQUEST['subaction'])) {
    $_POST = tep_db_prepare_input($_POST);

    if (tep_validate_email($_POST['email'])) {
      require_once(DIR_WS_FUNCTIONS . 'quote_forms.php');
      $_POST['state'] = tep_get_zone_name($_POST['country'], $_POST['state'], '');
      $_POST['country'] = tep_get_country_name($_POST['country']);
      save_form(TABLE_FORMS, 'Quote', STORE_OWNER_EMAIL_ADDRESS , $_POST['name'], $_POST['email'], array('company' => ENTRY_COMPANY, 'phone' => ENTRY_TELEPHONE_NUMBER, 'country' => ENTRY_COUNTRY, 'state' => ENTRY_STATE, array('manufacturer' => TEXT_MANUFACTURER, 'model' => TEXT_MODEL, 'quantity' => TEXT_QUANTITY, 'price' => TEXT_PRICE), 'need_quote' => TEXT_NEED_QUOTE, 'user' => TEXT_USER_I_AM_A, 'hear' => TEXT_HOW_DID_YOU_HEAR, 'hear_other' => TEXT_HOW_DID_YOU_HEAR_OTHER, 'enquiry' => ENTRY_ENQUIRY));
      tep_redirect(tep_href_link(FILENAME_QUOTE , '?action=thankyou'));
    } else {
      $error = true;
	$errorMess = ENTRY_EMAIL_ADDRESS_CHECK_ERROR;
      $messageStack->add('quote', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);
    }
  }

  if($_SESSION['quote']) {
    $quote = $_SESSION['quote'];
  } else {
    $quote = array('manufacturer' => array(), 'model' => array(), 'quantity' => array(), 'price' => array());
  }
$_POST = $_GET;
  if(((isset($_POST['subaction']) && $_POST['subaction'] == 'update')) || (isset($_REQUEST['action']) && $_REQUEST['action'] == 'send')) {
	  //echo 3;
    $quote = array('manufacturer' => array(), 'model' => array(), 'quantity' => array(), 'price' => array());
    foreach($_POST['manufacturer'] as $i => $m) {
    
      if((!empty($_POST['manufacturer'][$i]) || !empty($_POST['model'][$i])) && ($_POST['remove'][$i] != 1)) {
        $quote['manufacturer'][] = (isset($_POST['manufacturer'][$i]) ? $_POST['manufacturer'][$i] : '');
        $quote['model'][] = (isset($_POST['model'][$i]) ? $_POST['model'][$i] : '');
        $quote['quantity'][] = (isset($_POST['quantity'][$i]) ? $_POST['quantity'][$i] : '');
        $quote['price'][] = (isset($_POST['price'][$i]) ? $_POST['price'][$i] : '');
      }
    }
    //if(!$osC_Session->exists('quote')) {
    if (!tep_session_is_registered('quote')) {
    tep_session_register('quote');
      //$osC_Session->set('quote', $quote);
    }
    tep_redirect(tep_href_link(FILENAME_QUOTE, (isset($_POST['more']) && (int)$_POST['more'] > 0 ? 'more=' . (int)$_POST['more'] : '') ));
  } elseif(isset($_POST['manufacturer']) && is_array($_POST['manufacturer'])) {
	  //echo 4;
    foreach($_POST['manufacturer'] as $i => $m) {
      if((!empty($_POST['manufacturer'][$i]) || !empty($_POST['model'][$i])) && empty($_POST['remove'][$i])) {
        $quote['manufacturer'][] = (isset($_POST['manufacturer'][$i]) ? $_POST['manufacturer'][$i] : '');
        $quote['model'][] = (isset($_POST['model'][$i]) ? $_POST['model'][$i] : '');
        $quote['quantity'][] = (isset($_POST['quantity'][$i]) ? $_POST['quantity'][$i] : '');
        $quote['price'][] = (isset($_POST['price'][$i]) ? $_POST['price'][$i] : '');
      }
    }
    if (!tep_session_is_registered('quote')) {
    tep_session_register('quote');
    }
    tep_redirect(tep_href_link(FILENAME_QUOTE));
  }

  $_POST['manufacturer'] = $quote['manufacturer'];
  $_POST['model'] = $quote['model'];
  $_POST['quantity'] = $quote['quantity'];
  $_POST['price'] = $quote['price'];

  $breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_QUOTE));
?>

<?php
  require(DIR_WS_INCLUDES . 'header.php');
  //require(DIR_WS_INCLUDES . 'column_left.php');
  
  //1.1.94 quote extension start
  global $my;
  
  if ($my->id > 0 ) {
$customersq_query = tep_db_query("select c.customers_id, c.customers_gender, c.customers_firstname, c.customers_lastname, c.customers_dob, c.customers_email_address, c.customers_ap, a.entry_company, a.entry_company_tax_id, a.entry_street_address, a.entry_suburb, a.entry_postcode, a.entry_city, a.entry_state, a.entry_zone_id, a.entry_country_id, c.customers_telephone, c.customers_fax, c.customers_newsletter, c.customers_group_id,  c.customers_group_ra, c.customers_payment_allowed, c.customers_shipment_allowed, c.customers_default_address_id from " . TABLE_CUSTOMERS . " c left join " . TABLE_ADDRESS_BOOK . " a on c.customers_default_address_id = a.address_book_id where a.customers_id = c.customers_id and c.customers_id = '" . (int)$my->id . "'");
$customersq = tep_db_fetch_array($customersq_query);

        $qInfo = new objectInfo($customersq);
        echo $qInfo->entry_company;

        $qcompany = $qInfo->entry_company;
        $qphone = $qInfo->customers_telephone;
        $qcountry = $qInfo->entry_country_id; 
        $qzone = $qInfo->entry_zone_id; //edit
        $qstate = $qInfo->entry_state;
        
       // $qphone = $customersq['customers_telephone'];
       // echo $qphone;
        
  
  }
  if ($qcountry == '') {
  $qcountry = STORE_COUNTRY;
  }
  //1.1.94 quote extension end

  $a = array();
  $a['message_stack'] = ($messageStack->size('quote') > 0 ? $messageStack->output('quote') : '');
  $a['form'] = tep_draw_form('quote', tep_href_link(FILENAME_QUOTE, 'action=send'));

  if (isset($_GET['action']) && ($_GET['action'] == 'thankyou')) {
    $a['main_text'] = TEXT_SUCCESS;
    $a['button'] = tep_image_button('button_continue.gif', IMAGE_BUTTON_CONTINUE);
    $a['button_link'] = tep_href_link(FILENAME_DEFAULT);
  } else {
    $a['title'] = HEADING_TITLE;
    $a['name'] = ENTRY_NAME;
    $a['name_input'] = tep_draw_input_field('name', $mosUser['name'], 'style="width: 375px;"', true);
    $a['email'] = ENTRY_EMAIL;
    $a['email_input'] = tep_draw_input_field('email', $mosUser['email'], 'style="width: 180px;"', true);
    $a['company'] = ENTRY_COMPANY;
    $a['company_input'] = tep_draw_input_field('company', $qcompany, 'style="width: 375px;"', true);
    $a['phone'] = ENTRY_TELEPHONE_NUMBER;
    $a['phone_input'] = tep_draw_input_field('phone', $qphone, 'style="width: 180px;"', true);
    $a['country'] = ENTRY_COUNTRY;
    $a['country_input'] = tep_get_country_list('country', $qcountry, 'style="width: 185px;"');
    $a['state'] = ENTRY_STATE;

$a['state_input'] = tep_draw_input_field('state', $qstate, 'style="width: 180px;"', true);

//}

    // list
    $a['remove'] = 'Remove';
    $a['manufacturer'] = TEXT_MANUFACTURER;
    $a['model'] = TEXT_MODEL;
    $a['quantity'] = TEXT_QUANTITY;
    $a['price'] = TEXT_PRICE;
    $a['remove_input'] = array();
    $a['manufacturer_input'] = array();
    $a['model_input'] = array();
    $a['quantity_input'] = array();
    $a['price_input'] = array();

    $count = count($_POST['manufacturer']);
    if(isset($_GET['more']) && (int)$_GET['more'] > 0) {
      $count += ((int)$_GET['more'] + 5);
    } else {
      $count += 5;
    }

    for($i = 0; $i < $count; $i++) {
      $a['remove_input'][$i] = tep_draw_checkbox_field('remove['. $i .']', '1');
      $a['manufacturer_input'][$i] = tep_draw_input_field('manufacturer['. $i .']', (isset($_POST['manufacturer'][$i]) ? $_POST['manufacturer'][$i] : ''), 'style="width: 115px;"');
      $a['model_input'][$i] = tep_draw_input_field('model['. $i .']', (isset($_POST['model'][$i]) ? $_POST['model'][$i] : ''), 'style="width: 90px;"');
      $a['quantity_input'][$i] = tep_draw_input_field('quantity['. $i .']', (isset($_POST['quantity'][$i]) ? $_POST['quantity'][$i] : ''), 'style="width: 30px;"');
      $a['price_input'][$i] = tep_draw_input_field('price['. $i .']', (isset($_POST['price'][$i]) ? $_POST['price'][$i] : ''), 'style="width: 79px;"');
    }

    // need quote
    $a['need_quote'] = TEXT_NEED_QUOTE;
    $a['need_quote_input'] = tep_draw_pull_down_menu('need_quote', array(array('id' => '', 'text' => 'Please Select'),
                                                                         array('id' => 'Today', 'text' => 'Today'),
                                                                         array('id' => 'This Week', 'text' => 'This Week'),
                                                                         array('id' => 'Within A Month', 'text' => 'Within A Month'),
                                                                         array('id' => 'Within 6 Months', 'text' => 'Within 6 Months')  ),
                                                     '', 'style="width: 185px;"');
  
    

    $a['user'] = TEXT_USER_I_AM_A;
    $a['user_input'] = '';
    foreach(array('End-User', 'Integrator', 'Reseller', 'Other') as $user) {
      $a['user_input'] .= tep_draw_radio_field('user', $user) . '&nbsp;' . $user . '&nbsp;&nbsp;&nbsp;';
    }

    $a['hear'] = TEXT_HOW_DID_YOU_HEAR;
    $how_array = array('I am a Customer',
    'Our Newsletter',
    'Advertisement',
    'News Article',
    'Brochure',
    'Customer Referral',
    'EBay',
    'Google',
    'Yahoo!',
    'MSN',
    'Search Engines',
    'Trade Show',
    'Other');
    $how_array2 = array(array('id' => '', 'text' => 'Please Select'));
    foreach($how_array as $how) {
      $how_array2[] = array('id' => $how,
                            'text' => $how);
    }
    $a['hear_input'] = tep_draw_pull_down_menu('hear', $how_array2,
                                               '', 'style="width: 185px;"');

    $a['hear_other'] = TEXT_HOW_DID_YOU_HEAR_OTHER;
    $a['hear_other_input'] = tep_draw_input_field('hear_other', '', 'style="width: 200"');

    $a['enquiry'] = ENTRY_ENQUIRY;
    $a['enquiry_input'] = tep_draw_textarea_field('enquiry', 'hard', 50, 15, '', '');
    //echo '<pre>'; print_r($navigation->path); echo '</pre>';
    $back = sizeof($navigation->path)-2;
    //echo "<br><BR>$back<br>";
    if (isset($navigation->path[$back])) {
      $a['button_shopping_link'] = tep_href_link($navigation->path[$back]['page'], tep_array_to_string($navigation->path[$back]['get'], array('action')), $navigation->path[$back]['mode']);
      $a['button_shopping'] = tep_image_button('button_continue_shopping.gif', IMAGE_BUTTON_CONTINUE_SHOPPING);
    }
    //$a['update'] = tep_image_submit('button_update.gif', IMAGE_BUTTON_UPDATE, 'name="subaction" value="update"');
    $a['update'] = '<input type="submit" title=" Update " name="subaction" value="update" style="cursor: hand;" class="Button">';
    $a['more_button'] = '<input type="submit" title=" Continue " name="subaction" value=" Continue " style="cursor: hand;" class="Button">';
    //$a['more_button'] = tep_image_submit('button_continue.gif', IMAGE_BUTTON_CONTINUE, 'name="subaction" value="update"');
    $a['button'] = tep_image_submit('button_continue.gif', IMAGE_BUTTON_CONTINUE);
  }

  //to be coded in correctly via database
  
  require(WS_TEMPLATES . E_TEMPLATE_FOLDER . '/'.$osMod.'.html');


  require(DIR_WS_INCLUDES . 'footer.php');

  require(DIR_WS_INCLUDES . 'application_bottom.php');

  // -- webMS Modification --
  // reset the Mambo Database connection (stomped by osCommerce Connection)
  if (!DB_SHARED) {
    global $database, $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix;
    $database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
  }
 ?>